local niuma = fk.CreateSkill {

  name = "joy_mou__niuma",

  tags = {  },

}



niuma:addEffect("viewas", {
  name = "joy_mou__niuma",
  pattern = ".",
  expand_pile = function(self,player) 
    return player:getTableMark("joy_niuma")
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected == 0 then
      for _, cid in ipairs(player:getTableMark("joy_niuma")) do
        if cid == to_select then
          local card = Fk:getCardById(to_select)
          if Fk.currentResponsePattern == nil then
            return player:canUse(card) and not player:prohibitUse(card)
          else
            return Exppattern:Parse(Fk.currentResponsePattern):match(card)
          end
        end
      end
    end
  end,
  view_as = function(self, player,cards)
    if #cards ~= 1 then return end
    return Fk:getCardById(cards[1])
  end,
  enabled_at_play = function(self, player)
    local mark = player:getTableMark("joy_niuma")
    for _, cid in ipairs(mark) do
      local to_use = Fk:cloneCard(Fk:getCardById(cid).name)
      to_use.skillName = niuma.name
      if player:canUse(to_use) and not player:prohibitUse(to_use) then
        return true
      end
    end
    return false
  end,
  enabled_at_response = function(self, player, response)
    local mark = player:getTableMark("joy_niuma")
    if not response then
      for _, cid in ipairs(mark) do
        local to_use = Fk:cloneCard(Fk:getCardById(cid).name)
        to_use.skillName = niuma.name
        if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use) and
            not player:prohibitUse(to_use)  then
          return true
        end
      end
    else
      for _, cid in ipairs(mark) do
        local to_use = Fk:cloneCard(Fk:getCardById(cid).name)
        to_use.skillName = niuma.name
        if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use)  then
          return true
        end
      end
    end
    return false
  end,
})

niuma:addEffect(fk.RoundStart, {
  name = "#joy_mou__niuma_trigger",
  events = {fk.RoundStart,fk.EventPhaseEnd},
  can_trigger = function (self, event, target, player, data)
    if  not player:hasSkill(niuma.name) then return end
    if event == fk.RoundStart then
      local room = player.room
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:hasSkill(niuma,true) or p:hasSkill(niumaget,true) then
          return false
        end
      end
      return true
    elseif event == fk.EventPhaseEnd and target == player and player.phase == Player.Play  then
      local cards = player.room.tag["joy_niuma"] or {}
      return not player:isKongcheng() and #cards < 5 and player.hp > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local tar = room:askForChoosePlayers(player,table.map(room:getOtherPlayers(player),Util.IdMapper),1,1,"你可令一名其他角色获得〖流马〗",niuma.name,true)
      if #tar > 0 then
        event:setCostData(self, tar[1])
        return true
      end
    elseif event == fk.EventPhaseEnd then
      local cards = player.room.tag["joy_niuma"] or {}
      local n = math.min(player.hp,5 - #cards)
      local card = room:askForCard(player,1,n,false,niuma.name,true,".","你可将至多"..(n).."张手牌扣置于“流马”区")
      if #card > 0 then
        event:setCostData(self, card)
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      room:handleAddLoseSkills(room:getPlayerById(event:getCostData(self)),"joy_mou__niumaget")
    elseif event == fk.EventPhaseEnd then
      AddToNMPile(room,event:getCostData(self), niuma.name, player.id)
    end
  end,
})
niuma:addEffect(fk.EventPhaseEnd, {
  name = "#joy_mou__niuma_trigger",
  events = {fk.RoundStart,fk.EventPhaseEnd},
  can_trigger = function (self, event, target, player, data)
    if  not player:hasSkill(niuma.name) then return end
    if event == fk.RoundStart then
      local room = player.room
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:hasSkill(niuma,true) or p:hasSkill(niumaget,true) then
          return false
        end
      end
      return true
    elseif event == fk.EventPhaseEnd and target == player and player.phase == Player.Play  then
      local cards = player.room.tag["joy_niuma"] or {}
      return not player:isKongcheng() and #cards < 5 and player.hp > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local tar = room:askForChoosePlayers(player,table.map(room:getOtherPlayers(player),Util.IdMapper),1,1,"你可令一名其他角色获得〖流马〗",niuma.name,true)
      if #tar > 0 then
        event:setCostData(self, tar[1])
        return true
      end
    elseif event == fk.EventPhaseEnd then
      local cards = player.room.tag["joy_niuma"] or {}
      local n = math.min(player.hp,5 - #cards)
      local card = room:askForCard(player,1,n,false,niuma.name,true,".","你可将至多"..(n).."张手牌扣置于“流马”区")
      if #card > 0 then
        event:setCostData(self, card)
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      room:handleAddLoseSkills(room:getPlayerById(event:getCostData(self)),"joy_mou__niumaget")
    elseif event == fk.EventPhaseEnd then
      AddToNMPile(room,event:getCostData(self), niuma.name, player.id)
    end
  end,
})
niuma:addEffect(fk.AfterCardsMove, {
  refresh_events = {fk.AfterCardsMove,fk.AfterPlayerRevived,fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
      if player.seat == 1 and player.room.tag["joy_niuma"] then
        for _, move in ipairs(data) do
          if move.toArea ~= Card.Void then
            for _, info in ipairs(move.moveInfo) do
              if table.contains(player.room.tag["joy_niuma"], info.cardId) then
                return true
              end
            end
          end
        end
      end
    elseif event == fk.AfterPlayerRevived then
      return target == player and (player:hasSkill(niuma.name,true,true) or player:hasSkill("joy_mou__niumaget",true,true))
    else
      return target == player and table.contains({"joy_mou__niuma","joy_mou__niumaget"},data.name) and player:hasSkill(data.name,true)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.toArea ~= Card.Void then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(room.tag["joy_niuma"], info.cardId) then
              NotifyNMPile(room)
            end
          end
        end
      end
    else
      local mark = room.tag["joy_niuma"] or {}
      room:setPlayerMark(player,"joy_niuma",mark)
    end
  end,
})
niuma:addEffect(fk.AfterPlayerRevived, {
  refresh_events = {fk.AfterCardsMove,fk.AfterPlayerRevived,fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
      if player.seat == 1 and player.room.tag["joy_niuma"] then
        for _, move in ipairs(data) do
          if move.toArea ~= Card.Void then
            for _, info in ipairs(move.moveInfo) do
              if table.contains(player.room.tag["joy_niuma"], info.cardId) then
                return true
              end
            end
          end
        end
      end
    elseif event == fk.AfterPlayerRevived then
      return target == player and (player:hasSkill(niuma.name,true,true) or player:hasSkill("joy_mou__niumaget",true,true))
    else
      return target == player and table.contains({"joy_mou__niuma","joy_mou__niumaget"},data.name) and player:hasSkill(data.name,true)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.toArea ~= Card.Void then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(room.tag["joy_niuma"], info.cardId) then
              NotifyNMPile(room)
            end
          end
        end
      end
    else
      local mark = room.tag["joy_niuma"] or {}
      room:setPlayerMark(player,"joy_niuma",mark)
    end
  end,
})
niuma:addEffect(fk.EventAcquireSkill, {
  refresh_events = {fk.AfterCardsMove,fk.AfterPlayerRevived,fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
      if player.seat == 1 and player.room.tag["joy_niuma"] then
        for _, move in ipairs(data) do
          if move.toArea ~= Card.Void then
            for _, info in ipairs(move.moveInfo) do
              if table.contains(player.room.tag["joy_niuma"], info.cardId) then
                return true
              end
            end
          end
        end
      end
    elseif event == fk.AfterPlayerRevived then
      return target == player and (player:hasSkill(niuma.name,true,true) or player:hasSkill("joy_mou__niumaget",true,true))
    else
      return target == player and table.contains({"joy_mou__niuma","joy_mou__niumaget"},data.name) and player:hasSkill(data.name,true)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.toArea ~= Card.Void then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(room.tag["joy_niuma"], info.cardId) then
              NotifyNMPile(room)
            end
          end
        end
      end
    else
      local mark = room.tag["joy_niuma"] or {}
      room:setPlayerMark(player,"joy_niuma",mark)
    end
  end,
})

return niuma